<template>
  <el-dialog :close-on-click-modal='false' class="addEditForm" title="地区" :visible.sync="dialogFormVisible" :before-close="cancel">
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-row :gutter="10">
        <el-col :span="12">
          <el-form-item label="配送地：" prop="pickupPointName">
            <el-input type="input" placeholder="请输入配送地名称" v-model="form.pickupPointName" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注：" prop="remarks">
            <el-input type="input" placeholder="请输入备注" v-model="form.remarks" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="10">
          <el-form-item label="地址：" prop="provinceId">
            <el-select v-model="form.provinceId" filterable clearable placeholder="请选择省份">
              <el-option v-for="item in provinceOptions" :key="item.provinceId" :label="item.name"
                :value="item.provinceId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label-width="10px" prop="cityId">
            <el-select v-model="form.cityId" filterable clearable placeholder="请选择市">
              <el-option v-for="item in provinceCity" :key="item.cityId" :label="item.name" :value="item.cityId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="7">
          <el-form-item label-width="10px" prop="areaId">
            <el-select v-model="form.areaId" filterable clearable placeholder="请选择区">
              <el-option v-for="item in provinceArea" :key="item.areaId" :label="item.name" :value="item.areaId">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="详细地址：" prop="detailAddress">
            <el-input type="textarea" :rows="10" placeholder="请输入详细地址" v-model="form.detailAddress">
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import ProvincesCitiesDistricts from '@/api/module/provinces-cities-districts'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      required: false
    },
    api: {
      type: Function,
      required: true
    }
  },
  data () {
    return {
      provinceId: null, //省
      cityId: null,//市
      areaId: null,//区
      provinceOptions: [],//省
      provinceCity: [],//市
      provinceArea: [],//区
      dialogFormVisible: false,
      rules: {
        pickupPointName: [
          { required: true, message: '请输入自提点名称', trigger: 'blur' }
        ],
        remarks: [
          { required: true, message: '请输入备注', trigger: 'blur' }
        ],
        detailAddress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ],
        provinceId: [
          { required: true, message: '请选择省', trigger: 'blur' }
        ],
        cityId: [
          { required: true, message: '请选择市', trigger: 'blur' }
        ],
        areaId: [
          { required: true, message: '请选择区', trigger: 'blur' }
        ],
      },
      form: {
        pickupPointName: '',
        remarks: '',
        detailAddress: '',
        provinceId: null, //省
        cityId: null,//市
        areaId: null,//区
      },
    };
  },
  watch: {
    showDialog: {
      handler (v) {
        this.dialogFormVisible = v
        if (v) {
          this.form = { ...this.form, ...this.formData }
          console.log(this.form.area, '----------')
          if (this.form.area) {
            this.form.provinceId = this.form.area.city.province.provinceId || 1
            this.form.cityId = this.form.area.city.cityId || ''
            this.form.areaId = this.form.area.areaId || ''
          } else {
            this.form.provinceId = 1
          }
          this.queryProvincesList()
        }
      }
    },
    'form.provinceId': {
      handler (v) {
        console.log(v)
        if (v) {
          this.queryCityList()
        } else {
          this.form.areaId = ''
          this.form.cityId = ''
        }
      },
      deep: true
    },
    'form.cityId': {
      handler (v) {
        if (v) {
          this.queryAreaList()
        } else {
          this.form.areaId = ''
        }
      },
      deep: true
    }
  },
  methods: {
    //获取省份
    queryProvincesList () {
      ProvincesCitiesDistricts.queryProvince({
        provinceId: 0,
      }).then(res => {
        if (res.code == 200) {
          this.provinceOptions = res.data
          console.log(res.data, '省')
        } else {
          this.provinceOptions = []
        }
      })
    },
    //获取市
    queryCityList () {
      ProvincesCitiesDistricts.queryCity({
        provinceId: this.form.provinceId
      }).then(res => {
        if (res.code == 200) {
          this.provinceCity = res.data
          this.form.cityId = this.form.cityId || res.data[0].cityId
          console.log(res.data, '市')
        } else {
          this.provinceCity = []
        }
      })
    },
    //获取区
    queryAreaList () {
      ProvincesCitiesDistricts.queryArea({
        cityId: this.form.cityId
      }).then(res => {
        if (res.code == 200) {
          this.provinceArea = res.data
          this.form.areaId = this.form.areaId || res.data[0].areaId
          console.log(res.data, '区')
        } else {
          this.provinceArea = []
        }
      })
    },
    //关闭 isRef是否刷新表单
    close (isRef) {
      this.$emit('closeDialog', isRef)
      this.$refs.form.resetFields()
    },
    //取消
    cancel () {
      this.form = {
        pickupPointName: '',
        remarks: '',
        detailAddress: '',
        provinceId: null, //省
        cityId: null,//市
        areaId: null,//区
      }
      this.close()
    },
    //确认
    confirm () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          //  this.api 添加编辑接口 （注意传参）
          this.api(this.form).then(res => {
            if (res.code == 200) {
              this.$message({
                type: 'success',
                message: '操作成功!'
              });
              this.close(true)
            } else {
              this.$message({
                type: 'info',
                message: res.msg || '操作失败!'
              });
            }
          })
        } else {
          return false;
        }
      });
    }
  }
};
</script>
<style lang="scss" scoped>
::v-deep.el-select {
  width: 100%;
}
</style>